<?php

/**
 * catalog actions.
 *
 * @package    IPLIFE
 * @subpackage catalog
 * @author     alexey.shapran@gmail.com
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class catalogActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function preExecute()
	{
		$this->setLayout('cataloglayout');
	}

	public function executeIndex(sfWebRequest $request)
	{
		$numberofproductsperpage = 10;

		$this->categories = Category::getAllCategories();
		$this->pager = new sfDoctrinePager('Product', $numberofproductsperpage);

		$this->sort = $request->getParameter('sort', 'ASC');
		
		$variants = array('ASC' , 'DESC' , NULL);

		if(!in_array($this->sort, $variants))
			$this->redirect('default/index');

		$region_id = $request->getParameter('r_id');
		if($region_id)		
			$this->getUser()->setAttribute('region_id_for_catalog',$region_id);
		$this->region_id = $this->getUser()->getAttribute('region_id_for_catalog');

		$cat_id = $request->getParameter('catid');

		$query = Doctrine_Query::create()
    				->from("region")
    				->orderBy("name");

		$this->allregions = $query->execute();		
		
		if ( $cat_id == NULL)
		{
		
			$this->pager->setQuery(Product::gimmeAllProducts(null,$this->sort,$this->region_id));			
			$this->allcats = Category::getAllCategories();			
		}
		else
		{
			$this->pager->setQuery(Product::gimmeAllProducts($cat_id, $this->sort, $this->region_id));
			$this->cats = array();

			$this->cats[] = Category::getCategoryById($cat_id);			
			while (current($this->cats)->getParentId())
			{
				$this->cats[] = $this->getPrevParentCategory(current($this->cats));				
				end($this->cats);
			}
		}
		$this->pager->setPage($request->getParameter('page', 1));
		$this->pager->init();
		// Looking for course
		$filename = sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.'usd.txt';
		if(is_file($filename))
		{
			$file=fopen($filename, 'r+');
			$this->course = fread($file, 1000) / 100; // for 1 usd
			fclose($file);
		}
		else $this->course = 1;
	}

	protected function getPrevParentCategory($nowcat)
	{
		return Category::getCategoryById($nowcat->getParentId());
	}

	public function executeClearmycatalog(sfWebRequest $request)
	{
		if($this->getUser()->isAuthenticated())
		{
			$uid = $this->getUser()->getGuardUser()->getId();
			$allmypictures = Product::gimmePicturesToDeleteThem($uid);
			$path = sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.'products'.DIRECTORY_SEPARATOR;
				
			foreach ($allmypictures as $pic)
			{
				if(is_file($path.$pic['img']))
				{
					unlink($path.$pic['img']);
				}
			}
			Product::deleteAllMyProducts($uid);
		}

		$this->redirect('account/uploadprices');
	}

	public function executeShowproduct(sfWebRequest $request)
	{
		$prod_id = $request->getParameter('id');
		$this->prod = Product::gimmeProductIWantToSeeIt($prod_id);
		
		$this->cats = array();
		$this->cats[] = Category::getCategoryById($this->prod->getCategoryId());
		while (current($this->cats)->getParentId())
		{
			$this->cats[] = $this->getPrevParentCategory(current($this->cats));
			end($this->cats);
		}

		$filename = sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.'usd.txt';
		if(is_file($filename))
		{
			$file=fopen($filename, 'r+');
			$this->course = fread($file, 1000) / 100; // for 1 usd
			fclose($file);
		}
		else $this->course = 1;
		
		$this->seller = Doctrine::getTable('sfGuardUser')->findOneBy('id', $this->prod->getUserId());
	}
}